<template>
  <div class="grand-child">
    <h3>我是孙组件</h3>
    <h4>银子{{ money }}w</h4>
    <h4>车子：一辆{{ car.brand }}价值{{ car.price }}w</h4>
    <button @click="updateMoney(10)">花爷爷的money</button>
  </div>
</template>

<script setup lang="ts" name="GrandChild">
import { inject } from "vue";

// inject 获取祖先组件传递的值，可以使用默认值进行类型的推断
let { money, updateMoney } = inject("moneyContext", {
  money: 0,
  updateMoney: (n: number) => {},
});
console.log(money); // 获取的是Ref响应式对象，当祖先组件的数据发生改变了，这里也会自动更新
let car = inject("car", { brand: "未知", price: 0 });
</script>

<style scoped>
.grand-child {
  background-color: orange;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 0 10px black;
}
</style>
